package com.bt.springboot.web.service.impl;

import com.bt.springboot.common.util.CodeUtil;
import com.bt.springboot.entity.SMSCode;
import com.bt.springboot.web.service.SMSCodeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CachePut;
import org.springframework.stereotype.Service;

/**
 * @author zkx
 * @Date 2022/12/12 13:41
 */
@Service
public class SMSCodeServiceImpl implements SMSCodeService {

	@Autowired
	private CodeUtil codeUtil;

	@CachePut(key = "#phoneNumber",value = "cacheCode")
	@Override
	public String sendCode(String phoneNumber) {
		return codeUtil.generate(phoneNumber);
	}

	@Override
	public boolean checkCode(SMSCode smsCode) {
		// 取出内存中的验证码与传递过来的验证码比对，相同返回true
		String code = smsCode.getCode();
		String cacheCode = codeUtil.getCacheCode(smsCode.getPhoneNumber());
		return code.equals(cacheCode);
	}
}
